﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using TEILib;
using System.Collections;

namespace SPTEIProperties.EventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           //using(SPSite objSPSiteCollection = new SPSite("http://win-ss73o3vekal/"))
           //{
           //    //using(SPWeb objSPWeb = objSPSiteCollection.AllWebs[1])
           //    //{
           //    //    SPList objSPList = objSPWeb.Lists["XML"];
           //    //    foreach(SPListItem objItem in objSPList.Items)
           //    //    {
           //    //        objItem["Title"] = "Whatever";
           //    //    }
           //    //}

           //}
           ////MakeBibliography();  
           //try
           //{
               //SPSite objSPSiteCollection = SPContext.Current.Site;
           //    SPList objSPList = objSPSiteCollection.AllWebs[0].Lists["XML"];
           //    //SPQuery objSPQuery = new SPQuery();
           //    //objSPQuery

           //    foreach (SPListItem objSPListItem in objSPList.Items)
           //    {
           //        //get the bibliographic entry from the document
           //        //TEIDocument objTEIDocument = new TEIDocument(objSPListItem.Url);
           //        //ArrayList arrAuthor = objTEIDocument.author;
           //        String strTitle = "Whatever";// objTEIDocument.title;

           //        //update the fields in the document list

           //        //objSPListItem["Author(s)"] = arrAuthor[0];
           //        objSPListItem["Title"] = strTitle;

           //        //update the item
           //        objSPListItem.Update();

           //        //clean up objects
           //        //objTEIDocument = null;
           //        //arrAuthor = null;
           //        strTitle = null;
           //    }
           //}
           //catch (Exception ex)
           //{

           //    properties.Status = SPEventReceiverStatus.CancelWithError;
           //    properties.ErrorMessage = ex.Message;
           //    properties.Cancel = true;
           //}
       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {         
           base.ItemUpdating(properties);
           //MakeBibliography();
       }

       /// <summary>
       /// An item is being checked in.
       /// </summary>
       public override void ItemCheckingIn(SPItemEventProperties properties)
       {
           base.ItemCheckingIn(properties);
           //MakeBibliography();
       }

       /// <summary>
       /// An item is being unchecked out.
       /// </summary>
       public override void ItemUncheckingOut(SPItemEventProperties properties)
       {
           base.ItemUncheckingOut(properties);
           //MakeBibliography();
       }

        private void MakeBibliography()
        {
            try
            {
                SPSite objSPSiteCollection =SPContext.Current.Site;
                SPList objSPList = objSPSiteCollection.AllWebs[0].Lists["XML"];
                //SPQuery objSPQuery = new SPQuery();
                //objSPQuery
                
                foreach (SPListItem objSPListItem in objSPList.Items)
                {
                    //get the bibliographic entry from the document
                    TEIDocument objTEIDocument = new TEIDocument(objSPListItem.Url);
                    //ArrayList arrAuthor = objTEIDocument.author;
                    String strTitle = objTEIDocument.title;
                    
                    //update the fields in the document list
                    
                    //objSPListItem["Author(s)"] = arrAuthor[0];
                    objSPListItem["Title"] = strTitle;

                    //update the item
                    objSPListItem.Update();
                    
                    //clean up objects
                    objTEIDocument = null;
                    //arrAuthor = null;
                    strTitle = null;
                }
            }
            catch (Exception ex)
            {
                
                //properties.Status = SPEventReceiverStatus.CancelWithError;
                //properties.ErrorMessage = ex.Message;
                //properties.Cancel = true;
            }
            
        }
    }
}
